'use strict';

import A01DAL from './A01_Dal.js';
import B01DAL from './B01_Dal.js';
import A02DAL from './A02_Dal.js';
import A08DAL from './A08_Dal.js';
import A36DAL from './A36_Dal.js';
import A99z1DAL from './A99z1_Dal.js';
import util from './util.js';

import CryptoFile from "./CryptoFile.js";
import DAL from './dal.js'


//、、、、、、、、、、、、、、、、、、、、、、、、、、、
let A01DB;
let B01DB;
let A02DB;
let A08DB;
let A36DB;
let A99z1DB;
let utilTools;
let aCryptoFile;
let DALProcess;
export async function ddImport() {
	A01DB = new A01DAL();
	B01DB = new B01DAL();
	A02DB = new A02DAL();
	A08DB = new A08DAL();
	A36DB = new A36DAL();
	A99z1DB = new A99z1DAL();
	utilTools = new util();
	aCryptoFile = new CryptoFile();
	DALProcess = new DAL();
}



export function init() {
	ddImport().then(() => {
		console.log('初始化数据库成功');


		// DeleteDB();
		// makeAnalysisData();

		// let sdfsafawe = {
		// 	"Sex": "0",	// 性别		0.全部 1.男 2.女
		// 	"EthnicE": "0",	// 民族		0.全部 1.汉族 2.少数民族
		// 	"NativePlace": "",	// 籍贯
		// 	"PoliticsStatus": "0",	// 政治面貌	0.全部 1.中共党员 2.非中共党员
		// 	"PersonType": "",	// 个人身份	1.公务员 2.参公管理 3.事业单位职员
		// 	"FamilayMember": "",	// 家庭成员
		// 	"beginAgeRange": 20,	// 年龄段起始
		// 	"endAgeRange": 65,		// 年龄段结束
		// 	"ResumeInfo": "",	// 履历信息
		// 	"JobLevel": "",	// 职务		1.正处级 2.副处级 3.正科级 4.副科级

		// 	"EducationState": "0",	// 0.全部 1.在职 2.全职
		// 	"Education": "1,2",	// 学历 	1.研究生 2.大学 3.大专 4.中专
		// 	"beginStartingWorkYears": 0,	 // 参加工作年限起始
		// 	"endStartingWorkYears": 50,	 // 参加工作年限结束
		// 	"RewardsAndPunishment": ""	// 奖惩情况
		// }
		// AdvancedQuery(sdfsafawe).then((result)=>{
		// 	console.log(result)
		// });
		// getNOCCPInfos().then((rsdf)=>{
		// 	console.log(rsdf)
		// })
		// GetTingJiSimpleInfoArray().then((rsdf)=>{
		// 	console.log(rsdf)
		// })
		// GetPersionSimpleInfosByOrgID('001.001.001.004.003').then((result)=>{
		// 	console.log(result)
		// })

		// B01DB.getOrgTreeOrderInfo('001.001.001.001.00U').then((result)=>{
		// 	console.log(result)
		// })
		// SelectPeople("发改").then((result)=>{
		// 	console.log(result)
		// })
	}).catch((err) => {
		console.log('初始化数据库失败');
		console.log(err);
	});

}

// async function test(){
// 	let fullTimeHighSchoolCount = await A08DB.SelectPersionCountByEducationLevel('4', '1');
// 	console.log('全日制中专学历：' + fullTimeHighSchoolCount)
// 	let inServiceHighSchoolCount = await A08DB.SelectPersionCountByEducationLevel('4', '2');
// 	console.log('在职中专学历：' + inServiceHighSchoolCount)
// }
/////////////////////////////////////////////////////////////////
//#region 高级查询的内容
function getIntersection(aArray,bArray){
	var c = aArray.filter(function(v){ return bArray.indexOf(v) > -1 });
	return c;
}

function getLogicIntersection(aArray,bArray){
	if(aArray==null){
		//console.log(1)
		return bArray;
	}

	if(bArray==null){
		//console.log(2)
		return aArray;
	}
	//console.log(3)
	return getIntersection(aArray,bArray);
}
//高级查询 传入一个json的综合查询条件
//返回值与SelectPeople方法相同格式.这个暂不实现
export async function AdvancedQuery(advanceQueryKey) {


	//1性别 男 女 全部 如果是全部 就不搜了
	//let sexType = '男';
	let tempArray = null;
	// console.log(advanceQueryKey);
	// //查性别

	let sexArray = await getPersionIDArrayBySexForAdvance(advanceQueryKey)
	console.log(sexArray)
	//查民族
	let ethnicEArray = await getPersionIDArrayByEthnicForAdvance(advanceQueryKey)
	//console.log(ethnicEArray)

	tempArray = getLogicIntersection(sexArray,ethnicEArray);
	//console.log("tempArray")
	//console.log(tempArray)
	// //查籍贯
	let nativePlaceArray = await getPersionIDArrayByNativePlaceForAdvance(advanceQueryKey)
	//console.log(nativePlaceArray)
	tempArray = getLogicIntersection(tempArray,nativePlaceArray);
	//console.log(tempArray)
	//查政治面貌
	let politicsStatusArray = await getPersionIDArrayByPoliticsStatusForAdvance(advanceQueryKey)
	//console.log('politicsStatusArray')
	//console.log(politicsStatusArray)
	tempArray = getLogicIntersection(tempArray,politicsStatusArray);
	//console.log(tempArray)
	//个人身份  公务员 参照管理机关工作人员 事业单位管理人员 参照管理事业单位人员 其他人员 企业管理人员
	let persionTypeArray = await getPersionIDArrayByPersonTypeForAdvance(advanceQueryKey);
	//console.log('persionTypeArray')
	//console.log(persionTypeArray)
	tempArray = getLogicIntersection(tempArray,persionTypeArray);
	//console.log(tempArray)
	//家庭成员 "FamilayMember":
	let forFamilyArray = await getPersinIDArrayByFamilyForAdvance(advanceQueryKey);
	//console.log('forFamilyArray')
	//console.log(forFamilyArray)
	tempArray = getLogicIntersection(tempArray,forFamilyArray);
	//console.log(tempArray)
	//年龄段
	let ageArray = await getPersinIDArrayByAgeRangeForAdvance(advanceQueryKey);
	//console.log('ageArray')
	//console.log(ageArray)
	tempArray = getLogicIntersection(tempArray,ageArray);
	//console.log('ageArray')
	//console.log(tempArray)
	//职务
	let leaderLevelArray = await getPersinIDArrayByLeaderLevelForAdvance(advanceQueryKey);
	//console.log('leaderLevelArray')
	//console.log(leaderLevelArray)
	tempArray = getLogicIntersection(tempArray,leaderLevelArray);
	console.log(tempArray)

	//学历
	let educationPersionArray = await getPersionIDByEduForAdvance(advanceQueryKey);
	//console.log('educationPersionArray')
	//console.log(educationPersionArray)
	tempArray = getLogicIntersection(tempArray,educationPersionArray);
	//console.log(tempArray)
	// //console.log(educationPersionArray)
	// //参加工作年限
	let workingageArray = await getPersinIDArrayByWorkingageRangeForAdvance(advanceQueryKey);
	// console.log(tempArray)
	// console.log('workingageArray')
	// console.log(workingageArray)
	tempArray = getLogicIntersection(tempArray,workingageArray);

	//奖惩情况
	let RAPArray = await getPersionIDArrayByRewardsAndPunishmentForAdvance(advanceQueryKey);
	//console.log('RAPArray')
	//console.log(RAPArray)
	tempArray = getLogicIntersection(tempArray,RAPArray);
//	console.log(tempArray)
	// //console.log("tempArray")
	// //console.log(tempArray)

	if(tempArray==null){
		let persionInfosArray = await A01DB.SelectAllPersionSimpleInfos();
		return persionInfosArray;
	}else{
		let persionInfosArray = await A01DB.SelectPersionSimpleInfosByPersionIDArray(tempArray);
		//console.log(persionInfosArray)
		return persionInfosArray;
	}
	// //求全部数组的交集 空数组排除在外
}
/**
 * 听说这样原始的做法节省内存
 * @param {*} oldItemsArray
 * @param {*} newItemsArray
 */
function concatArray(oldItemsArray, newItemsArray) {
	if (newItemsArray && newItemsArray.length > 0) {
		for (let i = 0; i < newItemsArray.length; i++) {
			oldItemsArray.push(newItemsArray[i])
		}
	}
	return oldItemsArray;
}

/**
 * 高级查询查籍贯
 * @param {*} advanceQueryKey
 */
async function getPersionIDArrayByNativePlaceForAdvance(advanceQueryKey){
	let nativePlaceArray = [];
	if (advanceQueryKey.NativePlace != '') {
		nativePlaceArray = await A01DB.SelectPersonSimpleInfoByNativePlace(advanceQueryKey.NativePlace, true);
	}else{
		return null;
	}
	return getOnlyDateForIDArray(nativePlaceArray);
	//getOnlyDateForIDArray
}
/**
 * 高级查询查民族
 * @param {*} advanceQueryKey
 */
async function getPersionIDArrayByEthnicForAdvance(advanceQueryKey){
	let ethnicEArray = []
	if (advanceQueryKey.EthnicE != '0') {
		if (advanceQueryKey.EthnicE == '1') {
			//查汉族
			ethnicEArray = await A01DB.SelectPersonSimpleInfoByEthnic('汉族', true);
		} else if (advanceQueryKey.EthnicE == '2') {
			//查少数民族
			ethnicEArray = await A01DB.SelectMinorityPersonSimpleInfo(true);
		}
	}else{
		return null;
	}

	return getOnlyDateForIDArray(ethnicEArray);
	// return ethnicEArray;
}
/**
 * 高级查询查政治面貌
 * @param {*} advanceQueryKey
 */
async function getPersionIDArrayByPoliticsStatusForAdvance(advanceQueryKey){
	let politicsStatusArray = [];

	if(advanceQueryKey.PoliticsStatus == '0'){
		return null;
	}

	if (advanceQueryKey.PoliticsStatus == '1') {
		politicsStatusArray = await A01DB.SelectCCPSimplePersionInfo(true);
	}
	else if (advanceQueryKey.PoliticsStatus == '2') {
		politicsStatusArray = await A01DB.SelectNotCCPSimplePersionInfo(true);
	}
	return getOnlyDateForIDArray(politicsStatusArray);
}
/**
 * 高级查询查个人身份
 * @param {*} advanceQueryKey
 */
async function getPersionIDArrayByPersonTypeForAdvance(advanceQueryKey){
	let persionTypeArray = [];
	let persionTypeKey = [];

	if (advanceQueryKey.PersonType.indexOf('1') != -1) {
		persionTypeKey.push('公务员');
	}
	else if (advanceQueryKey.PersonType.indexOf('2') != -1) {
		persionTypeKey.push('参照管理机关工作人员');
	} else if (advanceQueryKey.PersonType.indexOf('3') != -1) {
		persionTypeKey.push('事业单位管理人员');
	}


	if (persionTypeKey.length > 0) {
		persionTypeArray = await A01DB.SelectSimplePersionInfoByPersonType(persionTypeKey, true);
	}
	else{
		return null;
	}

	return getOnlyDateForIDArray(persionTypeArray);
}

/**
 * 高级查询查家庭成员
 * @param {*} advanceQueryKey
 */
async function getPersinIDArrayByFamilyForAdvance(advanceQueryKey) {
	let familayMemberArray = [];
	let keyValue = advanceQueryKey.FamilayMember;
	if (keyValue != '') {
		familayMemberArray = await A36DB.SelectPersionIDByFamilyName(keyValue);
	}
	else{
		return null;
	}

	let onlyIDArray = new Array();
	for(let i=0;i<familayMemberArray.length;i++){
		onlyIDArray.push(familayMemberArray[i].SourcePersionID);
	}
	return onlyIDArray;

	//return getOnlyDateForIDArray(familayMemberArray);
}

/**
 * 查询指定的奖惩信息，为高级查询提供
 * @param {*} advanceQueryKey
 */
async function getPersionIDArrayByRewardsAndPunishmentForAdvance(advanceQueryKey) {
	let persionIDArray = []
	let rewardsAndPunishmentKey = advanceQueryKey.RewardsAndPunishment;

	if (rewardsAndPunishmentKey != '') {
		persionIDArray = await A01DB.SelectPersonSimpleInfoByRewardsAndPunishment(rewardsAndPunishmentKey, true)
	}else{
		return null;
	}

	return getOnlyDateForIDArray(persionIDArray);
}
/**
 * 返回指定工龄的人员ID数组
 * @param {*} advanceQueryKey
 */
async function getPersinIDArrayByWorkingageRangeForAdvance(advanceQueryKey) {
	let persionAgeArray = [];
	let lowerAge = advanceQueryKey.beginStartingWorkYears;
	let upperAge = advanceQueryKey.endStartingWorkYears;

	let lowerAgeDate = utilTools.getBrithDayString(lowerAge);
	let upperAgeDate = utilTools.getBrithDayString(upperAge);
	persionAgeArray = await A01DB.SelectSimplePersionInfoByWorkingAgeRange(upperAgeDate, lowerAgeDate, true)
	return getOnlyDateForIDArray(persionAgeArray);
}


/**
 * 返回指定年龄段的人员ID数组
 * @param {*} advanceQueryKey
 */
async function getPersinIDArrayByAgeRangeForAdvance(advanceQueryKey) {
	let persionAgeArray = [];
	let lowerAge = advanceQueryKey.beginAgeRange;
	let upperAge = advanceQueryKey.endAgeRange;
	let lowerAgeDate = utilTools.getBrithDayString(lowerAge);
	let upperAgeDate = utilTools.getBrithDayString(upperAge);
	persionAgeArray = await A01DB.SelectSimplePersionInfoByAgeRange(upperAgeDate, lowerAgeDate, true);
	return getOnlyDateForIDArray(persionAgeArray);

}
/**
 * 为高级查询的职务查询提供的方法
 */
async function getPersinIDArrayByLeaderLevelForAdvance(advanceQueryKey) {
	let leaderLevelPersionIDArray = [];
	let leaderLevelKey = advanceQueryKey.JobLevel;
	if(leaderLevelKey==''){
		return null;

	}

	let leaderLevelArray = [];
	if (leaderLevelKey.indexOf('1') != -1) {
		leaderLevelArray.push('县处级正职');
	}
	if (leaderLevelKey.indexOf('1') != -1) {
		leaderLevelArray.push('县处级副职');
	}
	if (leaderLevelKey.indexOf('1') != -1) {
		leaderLevelArray.push('乡科级正职');
	}
	if (leaderLevelKey.indexOf('1') != -1) {
		leaderLevelArray.push('乡科级副职');
	}

	leaderLevelPersionIDArray = await A01DB.SelectPersionSimpleInfoByLeaderLevel(leaderLevelArray, true);
	// console.log("leaderLevelPersionIDArray")
	// console.log(leaderLevelPersionIDArray)
	return getOnlyDateForIDArray(leaderLevelPersionIDArray)
}
/**
 * 为高级查询的学历查询提供的方法  去重也可以给图表方法一个参考
 * @param {} advanceQueryKey
 */
async function getPersionIDByEduForAdvance(advanceQueryKey) {
	let educationPersionArray = [];
	let educationStateKey = advanceQueryKey.EducationState;
	let educationKey = advanceQueryKey.Education;

	if(educationKey==''){
		return null;
	}
	let educationLevelParam = '3';//在职还是全日制 还是全部
	if (educationStateKey == '0') {
		educationLevelParam = '3'//不在乎 全部
	} else if (educationStateKey == '1') {
		educationLevelParam = '2'//在职
	} else if (educationStateKey == '2') {
		educationLevelParam = '1'//全日制
	}
//console.log("educationKey  "+educationKey)
	if (educationKey.indexOf('1') != -1) {
		//查研究生
		let tempArray = await A08DB.SelectPersionIDByEducationLevel('1', educationLevelParam);
		//console.log(tempArray)
		educationPersionArray = concatArray(educationPersionArray, tempArray);
	}

	if (educationKey.indexOf('2') != -1) {
		//查大学生
		//查研究生
		let tempArray = await A08DB.SelectPersionIDByEducationLevel('2', educationLevelParam);
		educationPersionArray = concatArray(educationPersionArray, tempArray);
	}

	if (educationKey.indexOf('3') != -1) {
		//查大专生
		let tempArray = await A08DB.SelectPersionIDByEducationLevel('3', educationLevelParam);
		educationPersionArray = concatArray(educationPersionArray, tempArray);
	}

	if (educationKey.indexOf('4') != -1) {
		//查高中生
		let tempArray = await A08DB.SelectPersionIDByEducationLevel('4', educationLevelParam);
		educationPersionArray = concatArray(educationPersionArray, tempArray);
	}
	//educationPersionArray = getOnlyDateForIDArray(educationPersionArray)

	let onlyIDArray = new Array();
	for(let i=0;i<educationPersionArray.length;i++){
		onlyIDArray.push(educationPersionArray[i].A0000);
	}

	educationPersionArray = utilTools.unique(onlyIDArray)
	return educationPersionArray;
}

/**
 * 为高级查询的性别查询提供的方法,如果查全部，
 * 则直接返回null，代表不参与筛查运算，调用方要做判断
 * @param {*} advanceQueryKey
 */
async function getPersionIDArrayBySexForAdvance(advanceQueryKey) {

	let sexArray = [];
	if (advanceQueryKey.Sex != '0') {
		let sexText = '';
		if (advanceQueryKey.Sex == '1') {
			//如果查男性
			sexText = '男'
		} else if (advanceQueryKey.Sex == '2') {
			//如果查女性
			sexText = '女'
		}

		if (sexText == '男' || sexText == '女') {
			sexArray = await A01DB.SelectPersionSimpleInfosBySex(sexText, true);

		}
		else {
			return [];
		}
	}
	else{
		return null;
	}

	return getOnlyDateForIDArray(sexArray);
	//return sexArray;

}

function getOnlyDateForIDArray(infoArray){
	let onlyIDArray = new Array();
	for(let i=0;i<infoArray.length;i++){
		onlyIDArray.push(infoArray[i].PersionID);
	}
	return onlyIDArray;
}

//#endregion
//得到两个数组的交集


//#region 常用查询

/**
 * 返回全部非党员人员
 */
export async function getNOCCPInfos(){
	return await A01DB.SelectNotCCPSimplePersionInfo(false);
}


/**
 * 返回全部女性干部简单信息
 */
export async function GetFemaleSimpleInfoArray() {
	return await A01DB.SelectPersionSimpleInfosBySex('女', false)
	//return await A01DB.SelectCountBySex('女');
}
/**
 * 返回厅级干部简单信息
 */
export async function GetTingJiSimpleInfoArray() {
	return await A01DB.SelectPersionSimpleInfoByLeaderLevel(['厅局级正职', '厅局级副职'], false)
	//return await A01DB.SelectCountBySex('女');
}

/**
 * 返回县级干部简单信息
 */
export async function GetXianJiSimpleInfoArray() {
	return await A01DB.SelectPersionSimpleInfoByLeaderLevel(['县处级正职', '县处级副职'], false)
	//return await A01DB.SelectCountBySex('女');
}

/**
 * 返回二级巡视员简单信息
 */
export async function GetErXunSimpleInfoArray() {
	return await A01DB.SelectPersionSimpleInfoByJobLevel(['二级巡视员'], false)
	//return await A01DB.SelectCountBySex('女');
}

/**
 * 返回科级干部简单信息
 */
export async function GetKeJiSimpleInfoArray() {
	return await A01DB.SelectPersionSimpleInfoByLeaderLevel(['乡科级正职', '乡科级副职'], false)
	//return await A01DB.SelectCountBySex('女');
}
/**
 * 返回四十岁以下县级干部信息（只查询县处级副职以上和四级调研员以上的），不包含四十
 */
export async function GetUnder40SimpleInfoArray() {
	//包含39岁还未到四十岁的 所以只要小于40岁都能查出来
	let ss = utilTools.getBrithDayString(40);//填写的年数是不包含
	return await A01DB.SelectSimplePersionInfoXian(ss, '20991231', false)
	//return await A01DB.SelectCountBySex('女');
}

/**
 * 返回少数民族干部信息
 */
export async function GetMinoritySimpleInfoArray() {
	return await A01DB.SelectMinorityPersonSimpleInfo(false)
	//return await A01DB.SelectCountBySex('女');
}
//#endregion



//keyValue是搜索词，包括姓名、工作单位、
//简称、拼音、拼音简称。
//比如：张三、zhangsan、zs
//发展与改革委员会、发改委、发展、fz、fgw
//之类的都能搜出来
//返回的是一个人员json
//人员ID暂定是要一个GUID的形式，是人员唯一的ID
export async function SelectPeople(keyValue) {
	let persionIDArray = new Array();

	// //1. 首先查家属姓名查到干部ID
	// let tempPersionIDArrayByFamily = await A36DB.SelectPersionIDByFamilyName(keyValue)

	// for(let tempkey in tempPersionIDArrayByFamily){
	// 	persionIDArray.push(tempPersionIDArrayByFamily[tempkey].SourcePersionID)
	// }

	//2.按单位名称查找单位ID
	let tempOrgIDArrayByOrgName = await B01DB.SelectOrgIDByOrgName(keyValue);

	// 3通过单位ID 查询单位在任的干部ID
	for (let i = 0; i < tempOrgIDArrayByOrgName.length; i++) {

		let temp = await A02DB.SelectPersionIDIncludBranchOrgByOrgID(tempOrgIDArrayByOrgName[i].OrgID);


		for (let tempkey in temp) {

			persionIDArray.push(temp[tempkey].PersionID)
		}
		console.log(temp)
	}

	//通过手机号码查人
	let temp = await A99z1DB.SelectPersionIDByTelNO(keyValue);

	for (let tempkey in temp) {

		persionIDArray.push(temp[tempkey].PersionID)
	}


	//4.通过干部姓名 查询干部姓名  干部姓名拼音  干部姓名拼音简写 查询到干部ID

	// let dString = aCryptoFile.encrypt(keyValue);
	// console.log(dString)

	keyValue = keyValue.toUpperCase()
	console.log(keyValue)
	let persionIDArrayByName = await A01DB.SelectPersionIDByPersionName(keyValue);//keyValue
	for (let tempkey in persionIDArrayByName) {
		persionIDArray.push(persionIDArrayByName[tempkey].PersionID)
	}



	persionIDArray = utilTools.unique(persionIDArray);

	let persionInfosArray = await A01DB.SelectPersionSimpleInfosByPersionIDArray(persionIDArray);

	return persionInfosArray;


}

// //要先各级别单位排序，之后再组织机构内排序
// export async function makeOnePersionSortInfo(PersionIDArray){
//
// }
//#region 图表分析

/**
 * 图表分析
 */
export async function makeAnalysisData() {
	//#region 综合人数
	let count = await A01DB.SelectCount();//总人数
	console.log(`count:${count}`);
	let maleCount = await A01DB.SelectCountBySex('男');//男性人数
	console.log(`maleCount:${maleCount}`);

	let femaleCount = count - maleCount;//女性人数
	console.log(`femaleCount:${femaleCount}`);


	let CCPCount = await A01DB.SelectCountByPoliticsStatus('中共党员');//党员人数
	console.log(`CCPCount:${CCPCount}`);
	let notCCP = count - CCPCount;//非党员人数
	console.log(`notCCP:${notCCP}`);

	let hanCount = await A01DB.SelectCountByEthnicE('汉族');//汉族人数
	console.log(`hanCount:${hanCount}`);
	let minorityCount = count - hanCount;//少数民族人数
	console.log(`minorityCount:${minorityCount}`);


	let doctorCount = await A08DB.SelectPersionCountByDegreeLevel('1', '3'); //标题头显示的博士学位人数
	console.log("doctorCount:" + doctorCount);

	let masterCount = await A08DB.SelectPersionCountByEducationLevel('1', '3'); //标题头显示的研究生学历人数
	console.log("masterCount:" + masterCount);
	//#endregion


	//#region 工作年限人数
	let count0_5 = await GetPersionCountByWorkingAge(0, 5);
	console.log("0-5年工龄的人数:" + count0_5);

	let count5_10 = await GetPersionCountByWorkingAge(5, 10);
	console.log("5-10年工龄的人数:" + count5_10);

	let count10_20 = await GetPersionCountByWorkingAge(10, 20);
	console.log("10-20年工龄的人数:" + count10_20);

	let count20_25 = await GetPersionCountByWorkingAge(20, 25);
	console.log("20-25年工龄的人数:" + count20_25);

	let count25_30 = await GetPersionCountByWorkingAge(25, 30);
	console.log("25-30年工龄的人数:" + count25_30);

	let count30_100 = await GetPersionCountByWorkingAge(30, 100);
	console.log("30年以上工龄的人数:" + count30_100);
	//#endregion

	//#region 在职全\日制学历

	let fullTimeGrabaiateCount = await A08DB.SelectPersionCountByEducationLevel('1', '1');
	console.log('全日制研究生学历：' + fullTimeGrabaiateCount)
	let inServiceGrabaiateCount = await A08DB.SelectPersionCountByEducationLevel('1', '2');
	console.log('在职研究生学历：' + inServiceGrabaiateCount)

	let fullTimeUndergraduateCount = await A08DB.SelectPersionCountByEducationLevel('2', '1');
	console.log('全日制本科学历：' + fullTimeUndergraduateCount)
	let inServiceUndergraduateCount = await A08DB.SelectPersionCountByEducationLevel('2', '2');
	console.log('在职本科学历：' + inServiceUndergraduateCount)

	let fullTimeUniversityCount = await A08DB.SelectPersionCountByEducationLevel('3', '1');
	console.log('全日制大专学历：' + fullTimeUniversityCount)
	let inServiceUniversityCount = await A08DB.SelectPersionCountByEducationLevel('3', '2');
	console.log('在职大专学历：' + inServiceUniversityCount)


	let fullTimeHighSchoolCount = count-fullTimeGrabaiateCount-fullTimeUndergraduateCount-fullTimeUniversityCount;//await A08DB.SelectPersionCountByEducationLevel('4', '1');
	console.log('全日制中专学历：' + fullTimeHighSchoolCount)
	let inServiceHighSchoolCount = 0;//await A08DB.SelectPersionCountByEducationLevel('4', '2');
	console.log('在职中专学历：' + inServiceHighSchoolCount)
	//#endregion


	//#region 年龄人数

	let count0_30_age = await GetPersionCountByAge(0, 30);
	console.log("小于三十岁人数:" + count0_30_age);

	let count30_35_age = await GetPersionCountByAge(30, 35);
	console.log("30-35岁人数:" + count30_35_age);

	let count35_45_age = await GetPersionCountByAge(35, 45);
	console.log("35-45岁人数:" + count35_45_age);

	let count45_55_age = await GetPersionCountByAge(45, 55);
	console.log("45-55岁人数:" + count45_55_age);

	let count55_100_age = await GetPersionCountByAge(55, 100);
	console.log("55岁以上人数:" + count55_100_age);
	//#endregion

	let maleRate = (maleCount && count) ? `${(maleCount / count * 100).toFixed(2)}%` : '0%';
	let femaleRate = (femaleCount && count) ? `${(femaleCount / count * 100).toFixed(2)}%` : '0%';
	let hanRate = (hanCount && count) ? `${(hanCount / count * 100).toFixed(2)}%` : '0%';
	let minorityRate = (minorityCount && count) ? `${(minorityCount / count * 100).toFixed(2)}%` : '0%';
	let CCPRate = (CCPCount && count) ? `${(CCPCount / count * 100).toFixed(2)}%` : '0%';
	let notCCPRate = (notCCP && count) ? `${(notCCP / count * 100).toFixed(2)}%` : '0%';


	return {
		info: {
			count: count || 0,   // 干部总人数
			male: maleCount || 0,    // 男性
			female: femaleCount || 0,  // 女性
			partyer: CCPCount || 0, // 党员
			unpartyer: notCCP || 0,// 非党员
			minority: minorityCount || 0, // 少数民族
			doctor: doctorCount || 0,  // 博士
			graduate: masterCount || 0 // 研究生
		},
		cards: {
			// 性别
			sex: [
				{ count: maleCount || 0, rate: maleRate },   // 男 count：人数  rate：百分比
				{ count: femaleCount || 0, rate: femaleRate }     // 女 count：人数  rate：百分比
			],
			// 民族
			nation: [
				{ count: hanCount || 0, rate: hanRate },   // 汉族 count：人数  rate：百分比
				{ count: minorityCount || 0, rate: minorityRate }     // 少数民族 count：人数  rate：百分比
			],
			// 党派
			party: [
				{ count: CCPCount || 0, rate: CCPRate },   // 党员 count：人数  rate：百分比
				{ count: notCCP || 0, rate: notCCPRate }     // 非党员 count：人数  rate：百分比
			]
		},
		// 工作年限图表
		WorkChart: [
			{ label: '30年以上', value: count30_100 || 0 },
			{ label: '25年-30年', value: count25_30 || 0 },
			{ label: '20年-25年', value: count20_25 || 0 },
			{ label: '10年-20年', value: count10_20 || 0 },
			{ label: '5年-10年', value: count5_10 || 0 },
			{ label: '5年以内', value: count0_5 || 0 },
		],
		// 在职学历/全日制学历图表
		EducationChart: [
			{
				type: '在职学历',
				data: [
					{ label: '研究生', value: inServiceGrabaiateCount || 0 },
					{ label: '本科', value: inServiceUndergraduateCount || 0 },
					{ label: '专科', value: inServiceUniversityCount || 0 },
					{ label: '高中及以下', value: inServiceHighSchoolCount || 0 },
				]
			},
			{
				type: '全日制学历',
				data: [
					{ label: '研究生', value: fullTimeGrabaiateCount || 0 },
					{ label: '本科', value: fullTimeUndergraduateCount || 0 },
					{ label: '专科', value: fullTimeUniversityCount || 0 },
					{ label: '高中及以下', value: fullTimeHighSchoolCount || 0 },
				]
			}
		],
		// 年龄图表
		AgeChart: [
			{ label: '', value: null },
			{ label: '小于30岁', value: count0_30_age || 0 },
			{ label: '30岁-35岁', value: count30_35_age || 0 },
			{ label: '35岁-45岁', value: count35_45_age || 0 },
			{ label: '45岁-55岁', value: count45_55_age || 0 },
			{ label: '55岁以上', value: count55_100_age || 0 }
		]
	}
}



//#endregion





//通过身份证号码查询人员主要信息
//参数是一个数组  ['410504197001010001','410504198001010001'] 代表两个人
//返回的和SelectPeople一样 是一个人员主要信息的json
export async function SelectPeopleByIDCardNO(IDCardNOArray) {
	return await A01DB.SelectPersionSimpleInfoByIDCardNO(IDCardNOArray, false)
}



//传入人员ID，返回任免表以及更多的信息 不包括家属信息，任免表需要调用这个和GetFamilyMembers
//这个做完了
export async function GetPeopleDetailInfo(PersonID) {
	let tempPersionIDArray = new Array();
	tempPersionIDArray.push(PersonID);
	let persionInfos = await A01DB.SelectPersionDetailInfosByPersionIDArray(tempPersionIDArray);
	return persionInfos;


}
//传入人员ID，返回家庭社会关系信息
//家庭内排序用于干部任免表里的列表显示顺序
export async function GetFamilyMembers(PersonID) {

	return await A36DB.SelectFamilyArrayByPersionID(PersonID)

}




/**
 *	返回全部组织机构
 * @param {num} type 1代表二级结构 2代表所有的法人单位都显示。现阶段固定死了只能是1
 */
export async function GetALLOrgStructure(type) {
	return await B01DB.SelectOrgTree();
}

/**
 * 返回机构本级和下级的全部人员简要信息 增加排序功能
 * 排序按照先总部后二级机构
 * 同级里按照机构内排序
 * @param {string} orgID 机构ID
 */
export async function GetPersionSimpleInfosByOrgID(orgID) {
	return await A01DB.GetPersionSimpleInfosIncludBranchOrgByOrgID(orgID);
	// let personInfoArray = new Array();

	// let orgIDArray = await B01DB.SelectOrgInfoByOrder(orgID);
	// //console.log(orgIDArray);//得到排序好的单位列表
	// for (let orgkey in orgIDArray){
	// 	let orgID = orgIDArray[orgkey].OrgID;
	// 	//console.log(orgID)
	// 	let personArray = await A02DB.SelectPersionIDByOrgID(orgID);//得到排好序的personIDArray
	// 	//console.log(personArray)
	// 	for(let personKey in personArray){
	// 		let personID = personArray[personKey].PersionID;
	// 		let personInfo = await A01DB.SelectPersionSimpleInfosByPersionID(personID);

	// 		if(personInfo.length>0){
	// 			let isHas = false;
	// 			for(let personKey2 in personInfoArray){

	// 				if(personInfoArray[personKey2].PersionID == personInfo[0].PersionID){
	// 					isHas = true;
	// 				}
	// 			}
	// 			if(!isHas){
	// 				personInfoArray.push(personInfo[0]);
	// 			}

	// 		}

	// 	}

	// 	//persionIDArray.push(persionID)
	// }
	// console.log(personInfoArray);
	// return personInfoArray;
	//A02DB.SelectPersionIDByOrgID()

	// let persionIDJsonArray = await A02DB.SelectPersionIDIncludBranchOrgByOrgID(orgID);

	// let persionIDArray = new Array();

	// for (let key in persionIDJsonArray) {
	// 	let persionID = persionIDJsonArray[key].PersionID
	// 	persionIDArray.push(persionID)
	// }

	// return await A01DB.SelectPersionSimpleInfosByPersionIDArray(persionIDArray);
}

/**
 * 通过persionid查他的电话号码 返回[{'PersionID':'xxx','telNO':'xxx'}]这种格式
 * @param {*} persionID
 */
export async function GetTelNOByPersionID(persionID) {
	return await A99z1DB.SelectTelNOByPersionID(persionID)
}


/**
 * 返回年龄区间的干部数量
 * @param {*} lowerAge  年龄下限 比如查找  35-45岁的 包含35 不包含45  左包含，右不包含
 * @param {*} upperAge 年龄上限
 */
export async function GetPersionCountByAge(lowerAge, upperAge) {
	let lowerAgeDate = utilTools.getBrithDayString(lowerAge);
	let upperAgeDate = utilTools.getBrithDayString(upperAge);
	return await A01DB.SelectCountByAgeRange(upperAgeDate, lowerAgeDate, false)
}


/**
 * 返回工龄区间的干部信息
 * @param {*} lowerAge  年龄下限 比如查找  10-15岁的 包含10 不包含15  左包含，右不包含
 * @param {*} upperAge 年龄上限
 */
export async function GetPersionCountByWorkingAge(lowerAge, upperAge) {
	let lowerAgeDate = utilTools.getBrithDayString(lowerAge);
	let upperAgeDate = utilTools.getBrithDayString(upperAge);
	return await A01DB.SelectCountByWorkingAgeRange(upperAgeDate, lowerAgeDate, false)
}

export async function DeleteDB(){
	DALProcess.deleteDate();
}
